<?php

/**
 * Casus form base class.
 *
 * @package    casesonline
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 16976 2009-04-04 12:47:44Z fabien $
 */
class BaseCasusForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'          => new sfWidgetFormInputHidden(),
      'title'       => new sfWidgetFormInput(),
      'image'       => new sfWidgetFormInput(),
      'description' => new sfWidgetFormTextarea(),
      'active'      => new sfWidgetFormInputCheckbox(),
      'created_at'  => new sfWidgetFormDateTime(),
      'source_list' => new sfWidgetFormPropelChoiceMany(array('model' => 'MultipleAnswer')),
      'status_list' => new sfWidgetFormPropelChoiceMany(array('model' => 'User')),
    ));

    $this->setValidators(array(
      'id'          => new sfValidatorPropelChoice(array('model' => 'Casus', 'column' => 'id', 'required' => false)),
      'title'       => new sfValidatorString(array('max_length' => 255)),
      'image'       => new sfValidatorString(array('max_length' => 255)),
      'description' => new sfValidatorString(),
      'active'      => new sfValidatorBoolean(),
      'created_at'  => new sfValidatorDateTime(array('required' => false)),
      'source_list' => new sfValidatorPropelChoiceMany(array('model' => 'MultipleAnswer', 'required' => false)),
      'status_list' => new sfValidatorPropelChoiceMany(array('model' => 'User', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('casus[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Casus';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['source_list']))
    {
      $values = array();
      foreach ($this->object->getSources() as $obj)
      {
        $values[] = $obj->getMultipleAnswerId();
      }

      $this->setDefault('source_list', $values);
    }

    if (isset($this->widgetSchema['status_list']))
    {
      $values = array();
      foreach ($this->object->getStatuss() as $obj)
      {
        $values[] = $obj->getUserId();
      }

      $this->setDefault('status_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveSourceList($con);
    $this->saveStatusList($con);
  }

  public function saveSourceList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['source_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(SourcePeer::CASUS_ID, $this->object->getPrimaryKey());
    SourcePeer::doDelete($c, $con);

    $values = $this->getValue('source_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new Source();
        $obj->setCasusId($this->object->getPrimaryKey());
        $obj->setMultipleAnswerId($value);
        $obj->save();
      }
    }
  }

  public function saveStatusList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['status_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(StatusPeer::CASUS_ID, $this->object->getPrimaryKey());
    StatusPeer::doDelete($c, $con);

    $values = $this->getValue('status_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new Status();
        $obj->setCasusId($this->object->getPrimaryKey());
        $obj->setUserId($value);
        $obj->save();
      }
    }
  }

}
